//
//  MKUITabBarItemCollectionViewCell.m
//  MKUI
//
//  Created by sushangjin on 16/3/29.
//  Copyright © 2016年 sushangjin. All rights reserved.
//

#import "MKUITabBarItemCollectionViewCell.h"
#import "UICollectionViewCell+MKUICollectionViewCellProtocol.h"
#import "UIColor+MKUI.h"
@implementation MKUITabBarItemCollectionViewCell
- (id)initWithFrame:(CGRect)frame{
	if (self=[super initWithFrame:frame]) {
		self.tabBarItemButton = [[MKUIFlowLayoutButton alloc] initWithContentStyle:MKUIFlowLayoutButtonContentStyleHorizontal];
		[self.tabBarItemButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateSelected];
		self.tabBarItemButton.contentInsets = [self.class contentInsets];
		self.tabBarItemButton.interitemSpacing = 5;
		[self.tabBarItemButton addTarget:self action:@selector(__onTabBarItemButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
		[self.contentView addSubview:self.tabBarItemButton];
        [self.tabBarItemButton setTitleColor:[UIColor mk_colorWithLight:[UIColor blackColor] dark:[UIColor whiteColor]] forState:UIControlStateNormal];
	}
	return self;
}
+ (UIEdgeInsets)contentInsets{
	return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (void)__onTabBarItemButtonDidTap:(id)sender{
	if(self.collectionCellModel.whenClick){
		self.collectionCellModel.whenClick(self.collectionCellModel);
	}
}
- (void)dealloc{
	NSArray<NSString *> *keypaths = @[@"title",@"image",@"enabled"];
	UITabBarItem *item = self.tabBarItem;
	for (NSString *keypath in keypaths) {
		[item removeObserver:self forKeyPath:keypath];
	}
}
- (void)customLayoutSubviews{
	[super customLayoutSubviews];
	CGRect bounds = self.contentView.bounds;
	//
	CGRect f1 = bounds;
	self.tabBarItemButton.frame = f1;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
	if(object==self.tabBarItem){
		[self reloadData];
	}
}
- (UITabBarItem *)tabBarItem{
    return self.collectionCellModel.modelValue;
}
- (void)reloadData{
	UITabBarItem *item = self.tabBarItem;
	[self.tabBarItemButton setTitle:item.title forState:UIControlStateNormal];
	[self.tabBarItemButton setImage:item.image forState:UIControlStateNormal];
	self.tabBarItemButton.enabled = item.enabled;
	self.tabBarItemButton.selected = self.collectionCellModel.selected;
}
- (void)setCollectionCellModel:(__kindof MKUICollectionViewCellModel *)collectionCellModel{
	NSArray<NSString *> *keypaths = @[@"title",@"image",@"enabled"];
	if(self.collectionCellModel&&!self.isSharedInstance){
		UITabBarItem *item = self.tabBarItem;
		for (NSString *keypath in keypaths) {
			[item removeObserver:self forKeyPath:keypath];
		}
	}
    [super setCollectionCellModel:collectionCellModel];
	if(!self.isSharedInstance){
		UITabBarItem *item = self.tabBarItem;
		for (NSString *keypath in keypaths) {
			[item addObserver:self forKeyPath:keypath options:NSKeyValueObservingOptionNew context:nil];
		}
	}
}
- (void)customReloadCellModel{
    [self reloadData];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    CGSize s = [self.tabBarItemButton sizeThatFits:CGSizeMake(99999999, 99999999)];
    s.height = MAX(44,s.height);
    return s;
}
- (CGRect)itemIndicatorRect{
    return [self convertRect:self.tabBarItemButton.titleLabel.frame fromView:self.tabBarItemButton];
}
@end
